package com.seadragon.app.ksr.validator;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.validator.routines.EmailValidator;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.seadragon.app.ksr.model.Business;
import com.seadragon.app.ksr.model.Office;
import static com.seadragon.app.ksr.model.KsrConstants.ERROR_MESSAGE_KEY_FIELD_REQUIRED;
import static com.seadragon.app.ksr.model.KsrConstants.DEFAULT_MESSAGE_KEY_INVALID_EMAIL;
import static com.seadragon.app.ksr.model.KsrConstants.FIELD_NAME_BUSINESS_NAME;
import static com.seadragon.app.ksr.model.KsrConstants.FIELD_NAME_CATEGORIES;
import static com.seadragon.app.ksr.model.KsrConstants.FIELD_NAME_EMAIL;
import static com.seadragon.app.ksr.model.KsrConstants.FIELD_NAME_PASSWORD;
import static com.seadragon.app.ksr.model.KsrConstants.FIELD_NAME_PHONE_NUMBER;
import static com.seadragon.app.ksr.model.KsrConstants.FIELD_NAME_SPECIALTY;
import static com.seadragon.app.ksr.model.KsrConstants.FIELD_NAME_DESCRIPTION;
import static com.seadragon.app.ksr.model.KsrConstants.FIELD_NAME_OFFICES;
import static com.seadragon.app.ksr.model.KsrConstants.ERROR_MESSAGE_KEY_OFFICE_REQUIRED;

public class BusinessValidator implements Validator {
	private final Validator officeValidator;
	public BusinessValidator(Validator officeValidator) {
        if (officeValidator == null) {
            throw new IllegalArgumentException(
              "The supplied [Validator] is required and must not be null.");
        }
        if (!officeValidator.supports(Office.class)) {
            throw new IllegalArgumentException(
              "The supplied [Validator] must support the validation of [Office] instances.");
        }
        this.officeValidator = officeValidator;
	}		
	
	
	@Override
	public boolean supports(Class<?> clazz) {
		return Business.class.isAssignableFrom(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
		Business bs = (Business) target;
		if (StringUtils.isEmpty(bs.getBusinessName()))
			errors.reject(FIELD_NAME_BUSINESS_NAME,ERROR_MESSAGE_KEY_FIELD_REQUIRED);
		if (StringUtils.isEmpty(bs.getPassword()))
			errors.reject(FIELD_NAME_PASSWORD,ERROR_MESSAGE_KEY_FIELD_REQUIRED);
		if (StringUtils.isEmpty(bs.getPhoneNumber()))
			errors.reject(FIELD_NAME_PHONE_NUMBER,ERROR_MESSAGE_KEY_FIELD_REQUIRED);
		if (StringUtils.isEmpty(bs.getSpecialty()))
			errors.reject(FIELD_NAME_SPECIALTY,ERROR_MESSAGE_KEY_FIELD_REQUIRED);
		if (StringUtils.isEmpty(bs.getDescription()))
			errors.reject(FIELD_NAME_DESCRIPTION,ERROR_MESSAGE_KEY_FIELD_REQUIRED);
		if (StringUtils.isEmpty(bs.getEmailAddress())){
			errors.reject(FIELD_NAME_EMAIL,ERROR_MESSAGE_KEY_FIELD_REQUIRED);
		} else if(!EmailValidator.getInstance().isValid(bs.getEmailAddress())){
			errors.reject(FIELD_NAME_EMAIL,DEFAULT_MESSAGE_KEY_INVALID_EMAIL);
		}		
		if (bs.getCategories()==null||bs.getCategories().size()<=0){
			errors.reject(FIELD_NAME_CATEGORIES,ERROR_MESSAGE_KEY_FIELD_REQUIRED);
		}
		if (bs.getOffices()==null||bs.getOffices().size()<=0){
			errors.reject(FIELD_NAME_OFFICES,ERROR_MESSAGE_KEY_OFFICE_REQUIRED);
		}
		//validate each office in offices list
        try {
            errors.pushNestedPath("office");
            if (bs.getOffices()!=null&&bs.getOffices().size()>0)
	            for (Office office : bs.getOffices()) {
	            	ValidationUtils.invokeValidator(this.officeValidator, office, errors);
			}
        } finally {
            errors.popNestedPath();
        }			
	}

}
